package cn.bs.busuo.front.account.service.impl;

import cn.bs.busuo.front.account.dao.persist.repository.IFavoriteRepository;
import cn.bs.busuo.front.account.pojo.entity.GoodFavorite;
import cn.bs.busuo.front.account.pojo.entity.HotelFavorite;
import cn.bs.busuo.front.account.pojo.entity.SightFavorite;
import cn.bs.busuo.front.account.pojo.vo.FavoriteGoodVO;
import cn.bs.busuo.front.account.pojo.vo.FavoriteHotelVO;
import cn.bs.busuo.front.account.pojo.vo.FavoriteSightVO;
import cn.bs.busuo.front.account.service.IFavoriteService;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class FavoriteServiceImpl implements IFavoriteService {
    @Autowired
    private IFavoriteRepository favoriteRepository;


    @Override
    public int addFavoriteHotel(Long hotelId, Long id) {
        //判断酒店收藏是否存在
        if (favoriteRepository.selectFavoriteHotelIfExits(hotelId,id) > 0) {
            String message = "添加用户收藏失败，已经添加过此收藏！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        HotelFavorite hotelFavorite = new HotelFavorite();
        hotelFavorite.setUserId(id);
        hotelFavorite.setHotelsId(hotelId);
        return  favoriteRepository.addFavoriteHotel(hotelFavorite);
    }

    @Override
    public int deleteFavoriteHotel(Long hotelId, Long id) {
        //判断酒店收藏是否存在
        if (favoriteRepository.selectFavoriteHotelIfExits(hotelId,id) == 0) {
            String message = "删除用户收藏失败，此收藏不存在！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        return favoriteRepository.deleteFavoriteHotel(hotelId,id);
    }

    @Override
    public List<FavoriteHotelVO> selectFavoriteHotel(Long id) {
        return favoriteRepository.selectFavoriteHotel(id);
    }

    @Override
    public int addFavoriteSight(Long sightId, Long id) {
        //判断景点收藏是否存在
        if (favoriteRepository.selectFavoriteSightIfExits(sightId,id) > 0) {
            String message = "添加用户收藏失败，已经添加过此收藏！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        SightFavorite sightFavorite = new SightFavorite();
        sightFavorite.setUserId(id);
        sightFavorite.setSightsId(sightId);
        return favoriteRepository.addFavoriteSight(sightFavorite);
    }

    @Override
    public List<FavoriteSightVO> selectFavoriteSight(Long id) {
        return favoriteRepository.selectFavoriteSight(id);
    }

    @Override
    public int deleteFavoriteSight(Long sightId, Long id) {
        //判断景点收藏是否存在
        if (favoriteRepository.selectFavoriteSightIfExits(sightId,id) == 0) {
            String message = "删除用户收藏失败，此收藏不存在！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        return favoriteRepository.deleteFavoriteSight(sightId,id);
    }

    @Override
    public int addFavoriteGood(Long goodId, Long id) {
        //判断商品收藏是否存在
        if (favoriteRepository.selectFavoriteGoodIfExits(goodId,id) > 0) {
            String message = "添加用户收藏失败，已经添加过此收藏！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        GoodFavorite goodFavorite = new GoodFavorite();
        goodFavorite.setUserId(id);
        goodFavorite.setGoodsId(goodId);
        return favoriteRepository.addFavoriteGood(goodFavorite);
    }

    @Override
    public List<FavoriteGoodVO> selectFavoriteGood(Long id) {
        return favoriteRepository.selectFavoriteGood(id);
    }

    @Override
    public int deleteFavoriteGood(Long goodId, Long id) {
        //判断商品收藏是否存在
        if (favoriteRepository.selectFavoriteGoodIfExits(goodId,id) == 0) {
            String message = "删除用户收藏失败，此收藏不存在！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        return favoriteRepository.deleteFavoriteGood(goodId,id);
    }


}
